package com.natsumehill.ls.rest;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.natsumehill.ls.po.UserPo;
import com.natsumehill.ls.service.SampleJvmService;
import com.natsumehill.ls.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class DemoController {

    //通过xml发布的，可使用@SofaReferencece注解
    //同时，如果需要通过@Autowired时，必须在当前模块中的bean配置文件中
    //通过<sofa:reference>标签声明引用，不然spring本身不可能跨模块进行bean的自动注入
    @SofaReference
    private SampleJvmService sampleJvmService;

    // 编程式发布，可通过注解调用
    @SofaReference(uniqueId = "serviceClientImpl")
    private SampleJvmService sampleJvmServiceClientImpl;

    @SofaReference(uniqueId = "annotationImpl")
    private SampleJvmService sampleJvmServiceAnnotationImpl;

    @SofaReference
    private UserService<UserPo> userService;

    @RequestMapping("/serviceWithoutUniqueId")
    public String serviceWithoutUniqueId() throws IOException {
        return sampleJvmService.message();
    }

    @RequestMapping("/annotationImplService")
    public String annotationImplService() throws IOException {
        return sampleJvmServiceAnnotationImpl.message();
    }

    @RequestMapping("/serviceClientImplService")
    public String serviceClientImplService() throws IOException {
        return sampleJvmServiceClientImpl.message();
    }

    @GetMapping("/queryUser")
    public String getUser(@RequestParam("name") String name){
        return userService.queryUser(new UserPo(name, null, null)).toString();
    }
}
